package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.user.entity.UserCategory;
import com.yike.user.mapper.UserCategoryMapper;
import com.yike.user.param.UserCategoryParam;
import com.yike.user.service.UserCategoryService;
import com.yike.user.param.UserCategoryPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.UserCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用户类型 服务实现类
 *
 * @author Sakura
 * @since 2024-10-31
 */
@Slf4j
@Service
public class UserCategoryServiceImpl extends BaseServiceImpl<UserCategoryMapper, UserCategory> implements UserCategoryService {

    @Autowired
    private UserCategoryMapper userCategoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUserCategory(UserCategoryParam userCategoryParam) throws Exception {
        // 验证类型名称是否已存在
        UserCategory userCategory = userCategoryMapper.selectOne(Wrappers.lambdaQuery(UserCategory.class)
                .eq(UserCategory::getName, userCategoryParam.getName()));
        if (userCategory != null) {
            throw new BusinessException("类型名称已存在");
        }

        userCategory = new UserCategory();
        BeanUtils.copyProperties(userCategoryParam, userCategory);
        userCategory.setCreateBy(LoginUtil.getUserId());
        return super.save(userCategory);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateUserCategory(UserCategoryParam userCategoryParam) throws Exception {
        UserCategory userCategory = userCategoryMapper.selectById(userCategoryParam.getId());
        if (userCategory == null) {
            throw new BusinessException("类型信息异常");
        }
        // 校验修改名称是否重复
        Integer checkNum = userCategoryMapper.selectCount(Wrappers.lambdaQuery(UserCategory.class)
                .eq(UserCategory::getName, userCategoryParam.getName())
                .ne(UserCategory::getId, userCategoryParam.getId()));
        if (checkNum > 0) {
            throw new BusinessException("类型名称已存在");
        }

        BeanUtils.copyProperties(userCategoryParam, userCategory);
        userCategory.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(userCategory);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteUserCategory(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public UserCategoryVo getUserCategory(Long id) throws Exception {
        return userCategoryMapper.getUserCategoryVo(id);
    }

    @Override
    public Paging<UserCategoryVo> getUserCategoryPageList(UserCategoryPageParam userCategoryPageParam) throws Exception {
        Page<UserCategory> page = new PageInfo<>(userCategoryPageParam);
        IPage<UserCategoryVo> iPage = userCategoryMapper.getUserCategoryList(page, userCategoryPageParam);
        return new Paging<UserCategoryVo>(iPage);
    }

}
